/**
 * 教练相关API
 */
import apiClient from './client';
import { BaseResponse } from '../types/response';
import { 
  CoachItem, HotCoachItem, CoachCommentItem, 
  CoachCommentFavoriteItem,CommentAttachmentItem,
  CommentFavoriteDataItem, CommentReplyParamterItem, 
  CommentAttachmentParamterItem, CoachPriceItem
} from '../types/coach';

/**
 * 根据基地ID获取教练列表
 * @param baseId 基地ID
 * @param page 页码
 * @param pageSize 每页数量
 * @returns 教练列表及分页信息
 */
export const getCoachesByBaseIdApi = async (
  baseId: number,
  page: number = 1,
  pageSize: number = 20
): Promise<BaseResponse<CoachItem[]>> => {
  return apiClient.get('/api/coach/v1/coaches', {
    base_id: baseId,
    page,
    page_size: pageSize
  });
};

/**
 * 获取当前基地下热门教练数据
 * @param baseId 基地ID
 * @param num 数量
 * @returns 热门教练列表
 */
export const getHotCoachesByBaseIdApi = async (
  baseId: number,
  num: number = 1000
): Promise<BaseResponse<HotCoachItem[]>> => {
  return apiClient.get('/api/coach/v1/hot', {
    base_id: baseId,
    num
  });
};

/**
 * 获取指定教练的详细信息
 * @param coachId 教练ID
 * @returns 教练详细信息
 */
export const getCoachDetailApi = async (
  coachId: number
): Promise<BaseResponse<CoachItem>> => {
  return apiClient.get('/api/coach/v1/info', {
    coach_id: coachId
  });
};

/**
 * 获取指定教练的价格套餐
 * @param coachId 教练ID
 * @returns 教练价格套餐列表
 */
export const getCoachPricesApi = async (
  coachId: number
): Promise<BaseResponse<CoachPriceItem[]>> => {
  return apiClient.get('/api/coach/v1/prices', {
    coach_id: coachId
  });
};

/**
 * 获取指定教练的荣誉墙
 * @param coachId 教练ID
 * @returns 教练荣誉墙信息
 */
export const getCoachHonorWallApi = async (
  coachId: number
): Promise<BaseResponse<any>> => {
  return apiClient.get('/api/coach/v1/honor-wall', {
    coach_id: coachId
  });
};

/**
 * 获取指定教练的评论列表
 * @param coachId 教练ID
 * @param page 页码
 * @param pageSize 每页数量
 * @returns 教练评论列表及分页信息
 */
export const getCoachCommentsApi = async (
  coachId: number,
  page: number = 1,
  pageSize: number = 10
): Promise<BaseResponse<{
  data: CoachCommentItem[];
  code: number;
  page: number;
  page_size: number;
  total: number;
  has_more: boolean;
}>> => {
  return apiClient.get('/api/feedback/v1/comment', {
    coach_id: coachId,
    page,
    page_size: pageSize
  });
};

/**
 * 获取指定用户对指定教练的评论列表
 * @param coachId 教练ID
 * @param page 页码
 * @param pageSize 每页数量
 * @returns 教练评论列表及分页信息
 */
export const getCoachUserCommentsApi = async (
  coachId: number
): Promise<BaseResponse<{
  data: CoachCommentItem;
  code: number;
}>> => {
  return apiClient.get('/api/feedback/v1/user-comment', {
    coach_id: coachId
  });
};

/**
 * 评论/回复点赞操作
 * @param favorite 点赞信息
 * @returns 教练信息
 */
export const setCoachCommentFavoriteApi = async (
  favorite: CoachCommentFavoriteItem
): Promise<{
  code: number;
  message: string;
  data: CommentFavoriteDataItem;
}> => {
  return apiClient.post('/api/feedback/v1/favorite', favorite);
};


/**
 * 获取评论详情数据
 * @param commentId 评论id
 * @param coachId 教练id
 * @returns 评论回复数据
 */
export const getCommentDetailApi = async (
  commentId: number,
  coachId: number
): Promise<{
  code: number;
  message: string;
  data: CoachCommentItem;
}> => {
  return apiClient.get('/api/feedback/v1/single', {
      comment_id: commentId,
      coach_id: coachId
  });
};

/**
 * 获取评论回复数据
 * @param commentId 评论id
 * @returns 评论回复数据
 */
export const getCommentRepliesApi = async (
  commentId: number,
  coachId: number,
  parentId: number = 0
): Promise<{
  code: number;
  message: string;
  data: CoachCommentItem[];
}> => {
  return apiClient.get('/api/feedback/v1/reply', {
      coach_id: coachId,
      comment_id: commentId,
      parent_id: parentId
  });
};

/**
 * 新增评论回复数据
 * @param commentId 评论id
 * @returns 评论回复数据
 */
export const addCoachCommentReplyApi = async (
  reply: CommentReplyParamterItem
): Promise<{
  code: number;
  message: string;
  data: CoachCommentItem;
}> => {
  return apiClient.post('/api/feedback/v1/reply', {
      coach_id: reply.coachId,
      content: reply.content,
      parent_id: reply.parentId,
      comment_id: reply.commentId,
      attachments: reply.attachments
    });
};

/**
 * 删除评论附件图片
 * @param commentId 评论id
 * @returns 评论回复数据
 */
export const deleteCommentAttachmentApi = async (
  image: CommentAttachmentParamterItem
): Promise<{
  code: number;
  message: string;
  data: CommentAttachmentItem[];
}> => {
  return apiClient.delete('/api/feedback/v1/image', {
      image_id: image.imageId,
      comment_id: image.commentId
  });
};

/**
 * 删除评论以及回复
 * @param coachId 教练id
 * @param commentId 评论id
 * @param isComment 是否删除评论
 * @returns 评论回复数据
 */
export const deleteCommentOrReplyApi = async (
  commentId: number,
  coachId: number,
  isComment: boolean = false
): Promise<{
  code: number;
  message: string;
  data: CoachCommentItem[];
}> => {
  return apiClient.delete('/api/feedback/v1/comment', {
      coach_id: coachId,
      comment_id: commentId,
      is_comment: isComment
  });
};


/**
 * 修改评论回复数据
 * @param commentId 评论id
 * @returns 评论回复数据
 */
export const updateCoachCommentReplyApi = async (reply: CommentReplyParamterItem): Promise<{code: number; message: string; data: CoachCommentItem;}> => {
  return apiClient.post('/api/feedback/v1/reply', {
      coach_id: reply.coachId,
      content: reply.content,
      score: reply.score,
      parent_id: reply.parentId,
      comment_id: reply.commentId,
      attachments: reply.attachments
    });
};

export const updateCoachCommentApi = async (reply: CommentReplyParamterItem): Promise<{code: number; message: string; data: CoachCommentItem;}> => {
  return apiClient.put('/api/feedback/v1/reply', {
      coach_id: reply.coachId,
      content: reply.content,
      score: reply.score,
      parent_id: reply.parentId,
      comment_id: reply.commentId,
      attachments: reply.attachments
    });
};

/**
 * 获取教练的工作时间
 * @returns 教练工作时间信息
 */
export const getAppointCoachWorktimeApi = async (): Promise<BaseResponse<any>> => {
  return apiClient.get('/api/appoint/v1/worktime');
};

/**
 * 获取教练的预约信息
 * @param coachId 教练ID
 * @param date 日期
 * @returns 预约结果信息
 */
export const getAppointApplyApi = async (
  coachId: number,
  date: string
): Promise<BaseResponse<any>> => {
  return apiClient.get('/api/appoint/v1/apply', {
    coach_id: coachId,
    date: date
  });
};

/**
 * 获取教练的会话id
 * @param coachId 教练ID
 * @param coachName 教练名称
 * @returns 会话id
 */
export const getCoachSessionIdApi = async (
  coachId: number,
  coachName: string
): Promise<BaseResponse<any>> => {
  return apiClient.get('/api/chat/v1/session_id', {
    coach_id: coachId,
    coach_name: coachName
  });
};

/**
 * 获取微信支付参数
 * @param amount 支付金额
 * @param description 支付描述
 * @returns 微信支付参数
 */
export const getWechatPayParamsApi = async (
  amount: number,
  description: string
): Promise<BaseResponse<{
  timeStamp: string;
  nonceStr: string;
  package: string;
  signType: string;
  paySign: string;
}>> => {
  return apiClient.post('/api/payment/v1/wechat', {
    amount: amount,
    description: description
  });
};

/**
 * 获取教练中心概览数据
 * @returns 教练中心概览数据
 */
export const getCoachCenterOverviewApi = async (): Promise<BaseResponse<any>> => {
  return apiClient.get('/api/coach-center/v1/overview');
};

/**
 * 获取教练预约数据
 * @returns 教练预约数据
 */
export const getCoachReservationsApi = async (): Promise<BaseResponse<any>> => {
  return apiClient.get('/api/coach-center/v1/reservations');
};

/**
 * 获取教练统计数据
 * @returns 教练统计数据
 */
export const getCoachStatsApi = async (): Promise<BaseResponse<any>> => {
  // 调用正确的统计数据接口
  return apiClient.get('/api/coach-center/v1/stats');
};

/**
 * 确认新学员
 * @param studentId 学员ID
 * @returns 确认结果
 */
export const confirmNewStudentApi = async (studentId: number): Promise<BaseResponse<any>> => {
  return apiClient.post('/api/coach-center/v1/confirm-student', {
    student_id: studentId
  });
};

